//
//  gameStateMaker.c
//  Dominion
//
//  Created by Eric Hook on 2/5/14.
//  Copyright (c) 2014 Eric Hook. All rights reserved.
//

#include "headers.h"

//#define NUM_TESTS 2
#define NUM_UNIQUE_CARDS 16
//#define TEXT_FEEDBACK = 0


int gameStateRandomizer (struct gameState *gs , int randSeed) {
    int defaultKingdomCards[10] = {adventurer, council_room, feast, gardens, mine,remodel, smithy, village, baron, great_hall}; //Default cards, as defined in playDom
    int treasureCards[3] = {copper, silver, gold};
    int i, numPlayers;

    int seed = randSeed;
    
    
    memset(gs, 0, sizeof(struct gameState));                 // clear the game state
    initializeGame(2, defaultKingdomCards, seed, gs);
    
    // Set up random gamestate//////////////
    gs->numPlayers = rand() % 3 + 2;                         //random reasonable (allowed) num players 2-4
    numPlayers = gs->numPlayers;
    
    for (i = 0; i < numPlayers; i++){
        
        ///// RANDOM deckCount /////
        if (rand()% 10 < -1){
            gs->deckCount[i] = rand() % MAX_DECK;            //random up to very large num deckCount some percent of time
        }
        else {
            //gs->deckCount[i]    = rand() % 30;             //OR a random common size deckCount
            gs->deckCount[i]    = 5;                         //OR a Fixed size deckCount
        }
        
        ///// RANDOM discardCount /////
        if (rand()% 10 < -1){
            gs->discardCount[i] = rand() % MAX_DECK;         //random up to very large num discardCount some percent of time
        }
        else {
            //gs->discardCount[i]    = rand() % 30;          //OR a random common size discardCount
            gs->discardCount[i]    = 0;                     //OR a Fixed size discardCount
        }
        
        ///// RANDOM handCount /////
        if (rand()% 10 < -1){
            gs->handCount[i] = rand() % MAX_HAND;            //random up to very large num handCount some percent of time
        }
        else {
            //gs->handCount[i]    = rand() % 10;             //OR a random common size handCount
            gs->handCount[i]    = 5;                         //OR a Fixed size handCount
        }
    }
    
    ///// RANDOM playedCardCount /////
    if (rand()% 10 < -1){
        gs->playedCardCount = rand() % MAX_DECK;             //random up to very large num handCount some percent of time
    }
    else {
        //G->playedCardCount[i]    = rand() % 3;           //OR a random common size handCount
        gs->playedCardCount    = 0;                          //OR a Fixed size handCount
    }
    
    gs->whoseTurn = rand() % numPlayers;
    gs->coins = 0;
    for (int i = 0; i < gs->numPlayers; i++){
        makeRandomHand(gs, i, gs->handCount[i]);                 // fill random card hand for each player
        makeRandomDeck(gs, i, gs->deckCount[i]);                 // fill random card deck for each player
        makeRandomDiscard(gs,i, gs->deckCount[i]);              // fill random card discard for each player
    }
    return 1;
}

int makeRandomHand (struct gameState *gs, int player, int numCards) {
    for (int i = 0; i < numCards; i++){
        if (rand()%10 < 2) {                                                    // balance/skew the hand with some Treasure cards (set to all treasure currently)
            gs->hand[player][i]=rand()% 3 + 4;                                    // cards 4, 5, and 6 are Copper Silver Gold
        }
        else gs->hand[player][i]=rand()% NUM_UNIQUE_CARDS;
        gs->handCount[player] = i;
    }
    return 0;
}

int makeRandomDeck (struct gameState *gs, int player, int numCards) {
    for (int i = 0; i < numCards; i++){
        if (rand()%10 < 2) {                                                    // balance/skew the deck with some Treasure cards (set to all treasure currently)
            gs->deck[player][i]=rand()% 3 + 4;                                    // cards 4, 5, and 6 are Copper Silver Gold
        }
        else gs->deck[player][i]=rand()% NUM_UNIQUE_CARDS;                        // fills rest of deck with overall random (includes additional treasure)
        gs->deckCount[player] = i;
    }
    
    return 0;
}

int makeRandomDiscard (struct gameState *gs, int player, int numCards) {
    for (int i = 0; i < numCards; i++){
        if (rand()%10 < 2) {                                                    // balance/skew the hand with some Treasure cards (set to all treasure currently)
            gs->discard[player][i]=rand()% 3 + 4;                                    // cards 4, 5, and 6 are Copper Silver Gold
        }
        else gs->discard[player][i]=rand()% NUM_UNIQUE_CARDS;
        gs->discardCount[player] = i;
    }
    return 0;
}

